package com.jxc.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import com.jxc.mapper.ProductMapper;
import com.jxc.mapper.ShopMapper;
import com.jxc.mapper.StockMapper;
import com.jxc.mapper.UserMapper;
import com.jxc.po.Product;
import com.jxc.po.Shops;
import com.jxc.po.Stocks;
import com.jxc.po.User;
import com.jxc.util.DBUtil;
import com.jxc.vo.ProductStock;
import com.jxc.vo.StockDetail;

import java.net.URLDecoder;


@Controller
public class StockAlertController  {
	  @RequestMapping(value="/stockalert")
	  //public void login(PrintWriter out) {  
	  public void login(PrintWriter out) {  
		SqlSession sqlSession = null;
		JSONObject jsonobject =null;	
		try {
			sqlSession = DBUtil.getSession();
			StockMapper stockMapper = sqlSession.getMapper(StockMapper.class);
			ShopMapper shopMapper = sqlSession.getMapper(ShopMapper.class);
			ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
			List<Stocks> list=new ArrayList<Stocks>();
			Map map = new HashMap();
			List<StockDetail> l=new ArrayList<StockDetail>();
			List<Shops> lshop=new ArrayList<Shops>();
			lshop=shopMapper.selectA();
			System.out.println(lshop.size());
			for(Shops shop:lshop){
				List<Stocks> lstock=new ArrayList<Stocks>();
				lstock=stockMapper.select(shop.getId());
				StockDetail sd=new StockDetail();
				sd.setShopname(shop.getName());
				if(lstock!=null){
					System.out.println(lstock.size());
					 List<ProductStock> lstockdetail=new ArrayList<ProductStock>();
				 	 for(Stocks stock:lstock){
				 		//System.out.println(stock.getProductid()+""+stock.getShopid()+""+stock.getId()+""+stock.getNum());
				 		ProductStock pd=new ProductStock();
						pd.setNum(stock.getNum());
						pd.setProductid(stock.getProduct_id());
						pd.setProductname(productMapper.selectbyid(stock.getProduct_id()).getName());
						lstockdetail.add(pd);
					}
					sd.setPd(lstockdetail);
				}
				l.add(sd);
			}
			if(l.size()==0){				  
				 map.put( "result", "null");       				 
				 jsonobject = JSONObject.fromObject( map ); 
			}else{
				 map.put( "result", l);       				 
				 jsonobject = JSONObject.fromObject( map ); 
 
			}
			sqlSession.commit();
		} catch (Exception e) {
			System.out.println(e.toString());
			sqlSession.rollback();
		} finally {
			DBUtil.closeSession(sqlSession);
		}
		System.out.println(jsonobject);
		out.print(jsonobject);
	  }  
	
}
